<template>
  <van-pull-refresh class="server" v-model="loading" @refresh="onRefresh">
    <van-list
      v-model:loading="loadingMore"
      :finished="finished"
      finished-text="没有更多了~"
      @load="onLoad"
    >
      <div class="banner">
        <van-swipe class="my-swipe" indicator-color="white">
          <van-swipe-item v-for="i in bannerList" @click="toBannerInfo(i)">
            <van-image fit="cover" :src="host + i.imageUrl" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="list">
        <div class="list_item" v-for="i in dataList" @click="toInfo(i)">
          <van-image fit="cover" :src="host + i.questionnaireImageUrl" />
          <div class="title">{{ i.questionnaireTitle }}</div>
        </div>
      </div>
    </van-list>
  </van-pull-refresh>
</template>

<script setup>
import { ref } from "vue";
import {
  queryServiceCarouselImageList,
  queryServiceQuestionnaireList,
} from "@/api/index";
import { useRouter } from "vue-router";
let router = useRouter();
let host = import.meta.env.VITE_APP_BASE_API;
let loading = ref(false);
function onRefresh() {
  loading.value = true;
  finished.value = false;
  loadingMore.value = true;
  dataList.value = [];
  page.value.pageNum = 1;
  getList();
  getBanner();
}
// 获取服务列表
let page = ref({
  pageNum: 1,
  pageSize: 10,
});
// 服务页
let bannerList = ref([]);
function getBanner() {
  queryServiceCarouselImageList().then((res) => {
    bannerList.value = res.data;
  });
}
getBanner();
// 加载更多
let loadingMore = ref(true);
let finished = ref(false);
function onLoad() {
  page.value.pageNum += 1;
  getList();
}
getList();

let dataList = ref([]);
function getList() {
  queryServiceQuestionnaireList(page.value).then((res) => {
    loading.value = false;
    loadingMore.value = false;
    dataList.value.push(...res.rows);
    if (res.rows.length < page.value.pageSize) {
      finished.value = true;
    }
  });
}

function toBannerInfo(row) {
  if (row.linkType == 1 || row.linkType == 2) {
    router.push("/txtInfo/" + row.linkUrl);
  } else if (row.linkType == 5) {
    location.href = row.linkUrl;
  }
}
// 跳转问卷详情
function toInfo(row) {
  if (row.questionnaireType == 1) {
    router.push("/queryDetails/" + row.id);
  } else {
    router.push("/questionDetail/" + row.id);
  }
}
</script>

<style scoped lang="scss">
.server {
  padding-bottom: 50px;
  height: calc(100vh - 50px);
  overflow: auto;
}
.list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 10px;
  .list_item {
    width: 49%;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    align-items: center;
    font-size: 14px;
    box-shadow: 1px 1px 9px #ddd;
    border-radius: 4px;
    overflow: hidden;
    .van-image {
      width: 100%;
      height: 173px;
      // height: 100%;
      // border-radius: 4px;
      overflow: hidden;
    }
    .title {
      text-align: center;
      white-space: nowrap; /* 确保文本在一行内显示 */
      overflow: hidden; /* 隐藏溢出的内容 */
      text-overflow: ellipsis; /* 使用省略号表示文本溢出 */
      height: 40px;
      line-height: 40px;
      // color: var(--el-color-primaryop9);
      color: #333;
      padding: 0 10px;
      font-size: 14px;
      font-weight: bold;
    }
  }
}
.banner {
  height: 160px;
  border-bottom: 1px solid #ccc;
}
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  height: 160px;
  text-align: center;
  background-color: #39a9ed;
  .van-image {
    width: 100%;
    height: 100%;
  }
}
</style>
